import 'package:flutter/material.dart';
import 'package:flutter_dva_hooks/flutter_dva_hooks.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:todo_app/components/Button/CustomElevateButton.dart';
import 'package:todo_app/components/FontIcon.dart';
import 'package:todo_app/components/TextExt.dart';
import 'package:todo_app/controllers/UserController.dart';
import 'package:todo_app/controllers/definitions/UserModel.dart';
import 'package:todo_app/hooks/MqtttUserClientController.dart';
import 'package:todo_app/layout/CustomCard.dart';
import 'package:todo_app/layout/Gaps.dart';
import 'package:todo_app/layout/Header.dart';
import 'package:todo_app/layout/Space.dart';
import 'package:todo_app/models/getters.dart';
import 'package:todo_app/pages/person/component/UserCard.dart';
import 'package:todo_app/utils/AppNavigator.dart';
import 'package:todo_app/utils/ExtHelper.dart';
import 'package:todo_app/utils/call/CallHelper.dart';
import 'package:todo_app/utils/mqtt/LocalMqttClient.dart';
import 'package:todo_app/utils/mqtt/UserDBUtils.dart';

class PersonDetailPage extends HookWidget {
  const PersonDetailPage({Key? key, required this.userId }) : super(key: key);
  final String userId;

  @override
  Widget build(BuildContext context) {
    var user = useState<UserModel>(UserModel());
    var store = useDvaStore();
    var mqttController = useMqtttUserClientController(userId.toInt());
    

    useEffect(() {
      UserDBUtils.queryUserById(userId.toInt()).then((value) {
        if(value != null) user.value = value;
      });
      UserController.userInfoUsingGet(userId).then((res) {
        if(res.status == 'success') {
          user.value = res.data!;
          if(user.value.pingPong != null) {
            mqttController.pingPong = UserClientPingPong.fromJson(user.value.pingPong!.toJson()) ;
            mqttController.setState((){});
          }
          UserDBUtils.saveUser(res.data!);
        }
      });
      return () {};
    }, []);

    return Scaffold(
      appBar: const Header(title: "", brightness: Brightness.light,),
      body: SingleChildScrollView(
        child: Column(
          children: [
            CustomCard(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  UserCard(user: user.value, pingPong: mqttController.pingPong),
                  Gaps.vGap12,
                  Space(
                    children: [
                      TextExt.desc(mqttController.pingPong != null ? 
                        "最近在线时间 ${DateTime.fromMillisecondsSinceEpoch(mqttController.pingPong!.time).toYMDHm()}" :
                        ""
                      )
                    ]
                  ),
                  Gaps.vGap12,
                  Space(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      CustomElevateButton(
                        width: 80.dp, 
                        height: 32.dp,
                        borderRadius: 0,
                        onTap: () {
                          AppNavigator.pushNamed("singleMessageRoom", params: { "id": userId });
                        },
                        child: Space(
                          padding: EdgeInsets.zero,
                          children: [
                            FontIcon(icon: FontIcons.bubble2, color: Colors.white, size: 16.dp,),
                            const Text("发消息", style: TextStyle(height: 1),)
                          ],
                        )
                      ),
                      CustomElevateButton(
                        width: 70.dp, 
                        height: 32.dp,
                        borderRadius: 0,
                        onTap: () {
                          // if(mqttController.pingPong?.online != true) {
                          //   showOverlayToast("用户不在线无法拨打");
                          //   return;
                          // }
                          CallUIHelper.call(store.rootState.system!.user!, user.value);
                        },
                        child: Space(
                          padding: EdgeInsets.zero,
                          children: [
                            FontIcon(icon: FontIcons.phone, color: Colors.white, size: 16.dp,),
                            const Text("拨打", style: TextStyle(height: 1),)
                          ],
                        )
                      )
                    ],
                  ),
                  Gaps.vGap24,
                ],
              )
            )
          ],
        ),
      ),
    );
  }
}